import java.util.ArrayList;
import java.util.List;

public class MovieServiceImpl implements MovieService {
    private List<Movie> movieList = new ArrayList<>();

    @Override
    public void addMovie(Movie movie) {
        movieList.add(movie);
        Logger.log("添加影片：" + movie.getTitle());
    }

    @Override
    public void deleteMovie(String name) {
        movieList.removeIf(m -> m.getTitle().equalsIgnoreCase(name));
        Logger.log("删除影片：" + name);
    }

    @Override
    public void updateMovie(Movie movie) {
        deleteMovie(movie.getTitle());
        addMovie(movie);
        Logger.log("更新影片：" + movie.getTitle());
    }

    @Override
    public List<Movie> queryMovie(String keyWord) {
        List<Movie> res = new ArrayList<>();
        for (Movie m : movieList) {
            if (m.getTitle().contains(keyWord) || m.getDirector().contains(keyWord) || m.getActor().contains(keyWord)) {
                res.add(m);
            }
        }
        return res;
    }

    @Override
    public List<Movie> listMovies() {
        return movieList;
    }
}